package pic;

import org.apache.commons.io.FilenameUtils;
import org.apache.commons.io.IOUtils;

import java.io.*;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 小汤圆照片分类自动化代码.
 *
 * @author sunhao(sunhao.java @ gmail.com)
 * @date 2018/1/20 上午1:27
 */
public class TangYuanPics {
    private static final String PIC_SRC = "/Users/sunhao/Downloads/pics/2018_07";
    private static final String PIC_DEST = "/Users/sunhao/Downloads/pic_dest";

    private static final Map<String, List<File>> PIC_STORE = new HashMap<>();
    private static final List<String> PIC_SUFFIX = Arrays.asList("jpg", "mp4", "mov");

    public static void main(String[] args) throws IOException, ParseException {
        File root = new File(PIC_SRC);
        List<File> pics = listFiles(root);

        System.out.println("共找到" + pics.size() + "张照片！");

        List<Date> dates = new ArrayList<>();
        for (File pic : pics) {
            String name = pic.getName();

            String year = name.substring(0, 4);
            String month = name.substring(4, 6);
            String day = name.substring(6, 8);

            String key = year + "-" + month + "-" + day;
            dates.add(new SimpleDateFormat("yyyy-MM-dd").parse(key));

            List<File> picStore = PIC_STORE.get(key);
            if (picStore == null) {
                picStore = new ArrayList<>();
            }

            picStore.add(pic);
            PIC_STORE.put(key, picStore);
        }

        Collections.sort(dates);

        Date first = dates.get(0);
        Date end = dates.get(dates.size() - 1);

        while (first.getTime() <= end.getTime()) {
            String key = new SimpleDateFormat("yyyy-MM-dd").format(first);
            String dest = PIC_DEST + "/" + key;
            List<File> picStore = PIC_STORE.get(key);
            if (picStore == null) {
                new File(dest + "空").mkdirs();
            } else {
                for (File pic : picStore) {
                    System.out.println("\t" + pic.getName());
                    File folder = new File(dest);
                    if (!folder.exists()) {
                        folder.mkdirs();
                    }

                    OutputStream os = new FileOutputStream(new File(folder, pic.getName()));
                    IOUtils.copy(new FileInputStream(pic), os);
                }
            }


            first = new Date(first.getTime() + 86400000);
        }
    }

    private static List<File> listFiles(File parent) {
        List<File> files = new ArrayList<>();
        File[] children = parent.listFiles();
        if (children == null || children.length == 0) {
            return files;
        }

        for (File child : children) {
            if (child.isDirectory()) {
                files.addAll(listFiles(child));
            } else if (child.isFile()) {
                if (!PIC_SUFFIX.contains(FilenameUtils.getExtension(child.getName().toLowerCase()))) {
                    continue;
                }
                files.add(child);
            }
        }

        return files;
    }
}
